<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css"
          media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/css/plat-style.css" media="all">
    <style>
        thead tr th {
            text-align: center !important;
        }

        tr td {
            text-align: center !important;
        }

    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body layui-form">
                    <%-- 查询条件设置 --%>
                    <div class="layui-container layui-form-item" style="padding-bottom: 50px">
                        <div class="layui-col-md12">
                            <div class="layui-col-md3">
                                <div class="layui-col-sm3" style="line-height: 38px">鸡舍标题：</div>
                                <div class="layui-col-sm8">
                                    <input class="layui-input" name="title" autocomplete="off" placeholder="支持模糊查询">
                                </div>
                            </div>
                            <div class="layui-col-md3">
                                <div class="layui-col-sm9 layui-col-sm-offset3">
                                    <button class="layui-btn layuiadmin-btn-list" lay-submit
                                            lay-filter="LAY-app-contlist-search">查询
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <%-- 数据展示 --%>
                    <table class="layui-table" id="table" lay-filter="table"></table>
<%--                    <script type="text/html" id="table-operate-tool">--%>
<%--                        <div class="layui-btn-container">--%>
<%--                            <button class="layui-btn layui-btn-warm" lay-event="add">新增鸡舍</button>--%>
<%--                        </div>--%>
<%--                    </script>--%>
                    <script type="text/html" id="toolbar">
                        <div class="layui-btn-container">
                            <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">编辑</a>
<%--                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="sellDetail">挂售详情</a>--%>
<%--                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="adoptionDetail">领养详情</a>--%>
<%--                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="delete">删除</a>--%>
                            {{# if(d.status == 1){ }}
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="updateStatus">禁用</a>
                            {{# } }}
                            {{# if(d.status == 0){ }}
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="updateStatus">启用</a>
                            {{# } }}
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'table'], function () {
        var $ = layui.$, layer = layui.layer, form = layui.form,
            element = layui.element, laydate = layui.laydate, table = layui.table;
        form.render('select');
        element.render();
        laydate.render({
            elem: '#laydate-cn',
            range: '~'
        });

        table.render({
            elem: '.layui-table',
            url: '/chickenRepository/handleSelect',
            page: true,
            toolbar: '#table-operate-tool',
            request: {
                pageName: 'current', //页码的参数名称，默认：page
                limitName: 'size' //每页数据量的参数名，默认：limit
            },
            cols: [[
                {field: 'id', title: 'ID', width: 120},
                {field: 'title', title: '鸡舍名称', width: 120},
                {field: 'level', title: '鸡舍等级', width: 120,
                    templet: function (d) {
                        if (d.level == 1) {
                            return 'V1'
                        } else if (d.level == 2) {
                            return 'V2'
                        }else if (d.level == 3) {
                            return 'V3'
                        }
                    }
                },
                {field: 'fraction', title: '所需创业分', width: 120},
                {field: 'price', title: '鸡苗单价', width: 120},
                {field: 'salePrice', title: '卖鸡单价', width: 120},
                {field: 'qty', title: '鸡苗总数量', width: 120},
                {field: 'totalPrice', title: '鸡苗总价', width: 120},
                {field: 'day', title: '孵化天数', width: 120},
                {field: 'status', title: '是否开启', width: 100,
                    templet: function (d) {
                        if (d.status == 0) {
                            return '否'
                        } else if (d.status == 1) {
                            return '是'
                        }
                    }
                },
                {
                    field: 'createTime', title: '创建时间', width: 150,
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm')}}</div>"
                },
                {title: '操作', templet: '#toolbar'}
            ]],
            skin: "row",
            even: true,
            size: "lg"
        });

        var active = {
            edit: function (id) {
                layer.open({
                    type: 2,
                    title:"编辑鸡舍",
                    area: ['700px', '600px'],
                    content: '/chickenRepository/chickenRepositoryEdit?id='+id,
                });
            },
            updateStatus:function (id,status) {
                layer.confirm('确定执行该操作么?',function(index){
                    $.ajax({
                        url: "/chickenRepository/handleUpdate",
                        data: {"id":id,"status":status},
                        dataType: "json",
                        type: "post",
                        success: function (data) {
                            if(data.status){
                                layer.close(index);
                                layer.msg(data.desc);
                                window.setTimeout(function () {
                                    window.location.reload();
                                }, 1000);
                            }else{
                                layer.close(index);
                                layer.msg(data.desc);
                            }
                        }
                    });
                });
            },
            sellDetail:function (id) {
                window.location.href="/chickenOrder/chickenOrder?crId="+id+"&orderType=6";
            },

            adoptionDetail:function (id) {
                window.location.href="/chickenOrder/chickenOrder?crId="+id+"&orderType=7";
            },
            delete: function (id,titile){
            layer.confirm('确定要删除【'+titile+"】该条记录么",function(index){
                $.ajax({
                    url: "/chickenRepository/handleDelete",
                    data: {"id":id},
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        if(data.status){
                            layer.close(index);
                            layer.msg(data.desc);
                            window.setTimeout(function () {
                                window.location.reload();
                            }, 1000);
                        }else{
                            layer.close(index);
                            layer.msg(data.desc);
                        }
                    }
                });
            });
        },
            exportThis: function (bid, orderType, payType, orderStatus, phone, orderCode, payCode, queryTime) {
                window.location.href = encodeURI("/export/handleExportOrder?queryTime=" + queryTime + "&payCode=" + payCode + "&orderCode=" + orderCode + "&phone=" + phone + "&payType=" + payType
                    + "&payType=" + payType + "&bid=" + bid + "&orderType=" + orderType + "&orderStatus=" + orderStatus);
            }
        };
        //导出事件触发
        $(".exportThis").on('click', function () {
            var exportThis = $(this).data('type');
            var bid = $('select[name="bid"]').find('option:selected').val();
            var orderType = $('select[name="orderType"]').find('option:selected').val();
            var payType = $('select[name="payType"]').find('option:selected').val();
            var orderStatus = $('select[name="orderStatus"]').find('option:selected').val();
            var phone = $('input[name="phone"]').val();
            var orderCode = $('input[name="orderCode"]').val();
            var payCode = $('input[name="payCode"]').val();
            var queryTime = $('input[name="queryTime"]').val();
            active[exportThis] && active[exportThis].call(this, bid, orderType, payType, orderStatus, phone, orderCode, payCode, queryTime);
        });


        table.on('toolbar(table)', function(obj){
            if(obj.event === 'add'){
                layer.open({
                    type: 2,
                    title:"新增鸡舍",
                    area: ['700px', '500px'],
                    content: "/chickenRepository/chickenRepositoryAdd",
                });
            }
        });

        //监听事件
        table.on('tool(table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'edit') {// 编辑
                active[layEvent] && active[layEvent].call(this, data.id);
            }else if (layEvent === 'delete') {// 删除
                active[layEvent] && active[layEvent].call(this, data.id,data.title);
            }else if (layEvent === 'add') {// 新增
                active[layEvent] && active[layEvent].call(this);
            }else if (layEvent === 'sellDetail' ) {// 挂售详情
                active[layEvent] && active[layEvent].call(this, data.id);
            }else if (layEvent === 'adoptionDetail') {// 领养详情
                active[layEvent] && active[layEvent].call(this,data.id);
            }else if (layEvent === 'updateStatus') {// 更改状态
                active[layEvent] && active[layEvent].call(this,data.id,data.status==1?0:1);
            }
        });

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {
                where: field
            });
        });


        //缓存当前操作的是哪个表格的哪个tr的哪个td
        $(document).off('mousedown', '.layui-table-grid-down').on('mousedown', '.layui-table-grid-down', function (event) {
            //直接记录td的jquery对象
            table._tableTrCurrr = $(this).closest('td');
        });

        //给弹出的详情里面的按钮添加监听级联的触发原始table的按钮的点击事件
        $(document).off('click', '.layui-table-tips-main [lay-event]').on('click', '.layui-table-tips-main [lay-event]', function (event) {
            var elem = $(this);
            var tableTrCurrr = table._tableTrCurrr;
            if (!tableTrCurrr) {
                return;
            }
            var layerIndex = elem.closest('.layui-table-tips').attr('times');
            layer.close(layerIndex);
            table._tableTrCurrr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
        });

    })
</script>
</body>
</html>